package petertel.pbx.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import petertel.pbx.connector.Executor;
import petertel.pbx.connector.IResult;

public class ParserUtils {

	private static final Pattern SECTION = Pattern
			.compile("^\\s*\\[\\s*(\\S+)\\s*\\]\\s*(\\([^)]+\\))?\\s*(;.*)?$");

	public static boolean isSection(String line) {
		Matcher matcher = SECTION.matcher(line);
		return matcher.matches();
	}

	public static String getSectionName(String line) {
		Matcher matcher = SECTION.matcher(line);
		if (!matcher.matches()) {
			throw new RuntimeException("Not a section: " + line);
		}
		return matcher.group(1).trim();
	}

	public static String getSectionTemplate(String line) {
		Matcher matcher = SECTION.matcher(line);
		if (!matcher.matches()) {
			throw new RuntimeException("Not a section: " + line);
		}
		String template = matcher.group(2);
		return template == null ? null : template.trim();
	}

	private static final Pattern COMMENT = Pattern.compile("^\\s*;.*$");

	public static boolean isComment(String line) {
		Matcher matcher = COMMENT.matcher(line);
		return matcher.matches();
	}

	private static final Pattern PAIR = Pattern
			.compile("^\\s*([^;=\\s]+)\\s*=>?([^;]+)*(;.*)?$");

	public static boolean isPair(String line) {
		Matcher matcher = PAIR.matcher(line);
		return matcher.matches();
	}

	public static String getPairName(String line) {
		Matcher matcher = PAIR.matcher(line);
		if (!matcher.matches()) {
			throw new RuntimeException("Not a pair: " + line);
		}
		return matcher.group(1).trim();
	}

	public static String getPairValue(String line) {
		Matcher matcher = PAIR.matcher(line);
		if (!matcher.matches()) {
			throw new RuntimeException("Not a pair: " + line);
		}
		return matcher.group(2).trim();
	}

	public static String readFile(Executor executor, String filename) {
		IResult result = executor.exec("/home/" + executor.getUsername()
				+ "/bin/readfile " + filename);
		if (result.getStatus() != 0) {
			throw new RuntimeException("[" + result.getStatus() + "] "
					+ result.getStderr());
		}
		return result.getStdout();
	}

	public static String[] readFile2(Executor executor, String filename) {
		IResult result = executor.exec("/home/" + executor.getUsername()
				+ "/bin/readfile " + filename);
		if (result.getStatus() != 0) {
			throw new RuntimeException("[" + result.getStatus() + "] "
					+ result.getStderr());
		}
		String conf = result.getStdout();
		BufferedReader breader = new BufferedReader(new StringReader(conf));
		List<String> lines = new ArrayList<String>();
		try {
			for (;;) {
				String line = breader.readLine();
				if (line == null) {
					break;
				}
				lines.add(line);
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return (String[]) lines.toArray(new String[0]);
	}
}
